package com.njupt.StringProblem;

/**
 *  leetcode 459. 重复的子字符串
 *
 *  枚举法：枚举字串的长度时1，2，3......(枚举的长度至n/2，字符串是枚举长度的整数倍)
 */
public class RepeatedSubstringPattern {

    public boolean repeatedSubstringPattern(String s) {

        int n = s.length();
        for (int i = 1; i <= n/2; i++) {
            if(n%i==0){
                boolean match = true;
                for (int j = i; j < s.length(); j++) {
                    if(s.charAt(j) != s.charAt(j-i)){
                        match = false;
                        break;
                    }
                }
                if(match){
                    return true;
                }
            }

        }
        return false;
    }

    public boolean repeatedSubstringPattern1(String s){
        int len = s.length();

        for (int i = 1; i <=len/2 ; i++) {
            if(len %i==0){
                boolean match =  true;
                String sub = s.substring(0,i);
                for (int j = 0; j < len/i; j++) {
                    if(!sub.equals(s.substring(j*i,j*i+i))){
                        match = false;
                        break;
                    }
                }
                if(match){
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {

        String str = "abab";

        RepeatedSubstringPattern test = new RepeatedSubstringPattern();

        System.out.println(test.repeatedSubstringPattern1(str));
    }
}
